package com.rc.saas.tenant.controller.ticket;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.ExcelExportUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.search.member.MemberTicketSearch;
import com.rc.saas.tenant.service.ticket.MemberTicketService;
import com.rc.saas.tenant.vo.ticket.MemberTicketExcelVo;
import com.rc.saas.tenant.vo.ticket.MemberTicketVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/6/30
 */
@Controller
@RequestMapping("/memberTicket")
public class MemberTicketController {
    private static Logger logger = LoggerFactory.getLogger(MemberTicketController.class);

    @Autowired
    private MemberTicketService memberTicketService;


    /**
     * 跳转至 会员优惠券详情
     */
    @RequestMapping(value = "/memberTicketDetail", method = RequestMethod.GET)
    public String memberTicketDetail(){
        return "/templates/ticket/memberTicket";
    }

    /**
     * 返回会员优惠券列表
     */
    @ResponseBody
    @RequestMapping(value = "/listPageMemberTicket", method = RequestMethod.GET)
    public WebPageInfo listPageMemberTicket(MemberTicketSearch memberTicketSearch){
        PageInfo<MemberTicketVo> pageInfo = memberTicketService.listPageMemberTicket(memberTicketSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 打印会员优惠券
     */
    @ResponseBody
    @RequestMapping("/exportMemberTicket")
    public TResponse exportMemberTicket(MemberTicketSearch memberTicketSearch, HttpServletResponse response) throws Exception {
        if (memberTicketSearch.getCount() == 0 || memberTicketSearch.getCount() == null) {
            memberTicketSearch.setCount(memberTicketSearch.getLimit());
        }
        if (memberTicketSearch.getCount() > 150000) {
            return TResponse.FAIL("最多导出15万数据");
        }

        //导出拼接开始条数
        memberTicketSearch.setOffset(memberTicketSearch.getLimit() * (memberTicketSearch.getOffset() - 1));

        List<MemberTicketVo> list = memberTicketService.exportMemberTicket(memberTicketSearch);
        List<MemberTicketExcelVo> printList = new ArrayList<>();
        try {
            for (MemberTicketVo memberTicketVo : list) {
                printList.add(new MemberTicketExcelVo(memberTicketVo));
            }
            ExcelExportUtil.downLoadExcel(response, printList, "会员优惠券", "会员优惠券");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("打印会员优惠券失败,msg:{}", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
        return TResponse.SUCCESS();

    }


}
